#include "dialog.h"
#include "ui_dialog.h"
#include <QInputDialog>
#include <QDebug>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    model = new QDirModel(this);
    // Enable modifying file system
    model->setReadOnly(false);
    model->setSorting(QDir::DirsFirst |
                      QDir::IgnoreCase |
                      QDir::Name);
    QModelIndex index = model->index("/home/luchu");
    if (!index.isValid())
        qDebug() << "InValid Index";
    else{
        ui->treeView->expand(index);
        ui->treeView->scrollTo(index);
        ui->treeView->setCurrentIndex(index);
    }

    ui->treeView->setModel(model);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_pushButton_clicked()
{
    // Make Dir
    QModelIndex index = ui->treeView->currentIndex();
    if (!index.isValid())
        return;

    QString name = QInputDialog::getText(this, "Name", "Enter a name");
    if (name.isEmpty())
        return;
    model->mkdir(index, name);
}

void Dialog::on_pushButton_2_clicked()
{
    // Remove Dir
    QModelIndex index = ui->treeView->currentIndex();
    if (!index.isValid())
        return;

    if (model->fileInfo(index).isDir()){
        model->rmdir(index);
    }else{
        model->remove(index);
    }
}
